package com.sbr.seb.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.Log;


public class LogReader {

	private static final Logger logger = LoggerFactory.getLogger(LogReader.class);
	private static final String DEFAULT_DELIMITER = "-";
	private String delimiter;
	private StringBuilder sb;
	
	
	public LogReader() {
		sb = new StringBuilder();
		delimiter = DEFAULT_DELIMITER;
	}
	
	public List<Log> read(BufferedReader in) {
		String inputLine = null;
		List<Log> logList = new ArrayList<Log>();
		try {
			while ((inputLine = in.readLine()) != null) {
				logger.debug("reading line is : "+inputLine.trim());
				
				if (inputLine.trim().startsWith("INFO")) {
					logger.debug("starting new Log line");
					
					if (!sb.toString().isEmpty()) {
						logList.add(new Log(sb.toString()));
					}
					sb = new StringBuilder();
					// FIX ME problème première ligne
				}
				
				sb.append(inputLine);
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return logList;
	}
	
	/**
	 * 
	 * INFO  - 22/12/2009 11:40:46 564 - Loading validation rules file from '/WEB-INF/validator-rules.xml'
	 * 
	 */
	
	private Log readLine(String line) {
		logger.debug(line);
		if (line.trim().startsWith("INFO")) {
			sb = new StringBuilder();
		}
		sb.append(line);
//		StringTokenizer st = new StringTokenizer(line,delimiter);
//		
//		
//		
//		String level = st.nextToken().trim();
//		logger.debug(level);
//		String timestamp = st.nextToken().trim();
//		logger.debug(timestamp);
//		String message = st.nextToken().trim();
//		logger.debug(message);

		Log log =null;
		
//		log.setLevel(level);
//		log.setTimestamp(timestamp);
//		log.setMessage(message);
		
		return log;
		
	}

	public void setDelimiter(String delimiter) {
		this.delimiter = delimiter;
	}
	
	
	
}
